package com.itheima.controller;

import com.itheima.config.ServerConfig;
import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSOperator;
import com.itheima.utils.FileUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private FileUploadUtils fileUploadUtils;

    /**
     * 文件上传（上传到本地磁盘）
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("文件上传：{}",file);

        //保存文件，并且返回文件访问路径“/image/tails/2025/06/10/baf42e72-58a1-4db0-8250-990ef8ef9c94.png”
        String newPathName = fileUploadUtils.upload(file);

        //拼接文件访问路径
        String url = serverConfig.getUrl()+newPathName;

        return Result.success(url);
    }

//    @Autowired
//    private AliyunOSSOperator aliyunOSSOperator;

//    @PostMapping("/upload")
//    public Result upload(MultipartFile file) throws Exception {
//        log.info("文件上传：{}",file.getOriginalFilename());
//       //将文件交给OSS管理
//        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
//        log.info("文件上传成功，文件访问路径：{}",url);
//        return Result.success(url);
//    }
}
